//
//  GeneralAuditCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/3/8.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit
enum AuditCoTypeEnum {
    // 内容类型: 纯内容  分割线  文件类型  报表类型  带下划线跳转的文字
    case content, seperate, file, report, underLine, more
}
class AuditItemCell: UITableViewCell {
    
    private lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 3
        return v
    }()
    private  lazy var titleLab:UILabel = {
        let v = UILabel()
        v.textColor = UIColor(kshex:"888888")
        v.numberOfLines = 0
        return v
    }()
    lazy var mustLab:UILabel = {
        let v = UILabel()
        v.textColor = UIColor.red
        v.text = "*"
        v.font = UIFont.systemFont(ofSize: 20)
        return v
    }()
    lazy var contentLab:UITextView = {
        let v = UITextView()
        v.font = UIFont.systemFont(ofSize: 16)
        v.textAlignment = .right
        v.backgroundColor = UIColor(kshex: "f5f5f5")
        return v
    }()
    
    private lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#e5e5e5")
        return v
    }()
    private lazy var icon:UIImageView = {
        let v = UIImageView()
        return v
    }()
    var editable = false {
        didSet {
            contentLab.isEditable = editable
            contentLab.isUserInteractionEnabled = editable
            if editable {
                contentLab.backgroundColor = UIColor(kshex: "f5f5f5")
            } else {
                contentLab.backgroundColor = .white

            }
        }
    }
    var conType:AuditCoTypeEnum = .content {
        didSet {
            self.titleLab.text = titlex
            
            
     
                
                if contentx.contains("<span") || contentx.contains("<p") {
                    do {
                        if let data = contentx.data(using: String.Encoding.unicode, allowLossyConversion: true) {
                            let attStr = try NSAttributedString.init(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,], documentAttributes: nil)
                            self.contentLab.attributedText = attStr
                        }
                    } catch {
                        self.contentLab.attributedText = nil
                         self.contentLab.text = contentx
                    }
                } else {
                    self.contentLab.attributedText = nil
                     self.contentLab.text = contentx
                }
                 
                
                titleLab.snp.remakeConstraints { make in
                    make.left.equalTo(bg).offset(8)
                    make.centerY.equalTo(bg)
                    make.width.equalTo(100)
                }
            
            // 手动计算文本高度
            var hhh = contentx.heightWithStringAttributes(attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 17)], fixedWidth: KSGlobal.screenW - 140) + 25
            if hhh < ItemHHeight {
                hhh = ItemHHeight
            }
                
                contentLab.snp.remakeConstraints { make in
                    make.right.equalTo(bg).offset(-15)
                    make.top.equalTo(bg).offset(6)
                    make.left.equalTo(titleLab.snp.right).offset(10)
                    make.height.equalTo(hhh)
                    make.bottom.equalTo(bg).offset(-6)
                }
            
            
            
            

            // 标题处理颜色
            switch conType {
            case .content:
                icon.isHidden = true
                contentLab.textColor = .black
                contentLab.isUserInteractionEnabled = true

            case .file:
                icon.isHidden = false
                 contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                contentLab.isUserInteractionEnabled = false
                if contentx.contains("pdf") {
                    icon.image = UIImage(named: "icon_pdf")
                } else if contentx.contains("doc") {
                    icon.image = UIImage(named: "icon_word")
                } else if contentx.contains("xls") {
                    icon.image = UIImage(named: "icon_excel")
                } else {
                    icon.image = UIImage(named: "icon_pic")
                }
             case .report:
                icon.isHidden = false
                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                icon.image = UIImage(named: "icon_report")
            case .underLine:
//                icon.isHidden = true
//                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
//                contentLab.underline()
                break
            default:
                break
            }
     
            
            
        }
    }
    var titlex = ""
    var contentx = ""
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = UIColor.clear
        bg.backgroundColor = .white
        self.contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(6)
            make.top.equalTo(self.contentView)
            make.right.equalTo(self.contentView).offset(-6)
            make.bottom.equalTo(self.contentView)
        }
        bg.addSubview(titleLab)
        
        
        
        bg.addSubview(mustLab)
        
        bg.addSubview(contentLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.centerY.equalTo(bg)
            make.width.equalTo(80)
        }
        mustLab.snp.makeConstraints { make in
            make.right.equalTo(titleLab.snp.left)
            make.centerY.equalTo(titleLab)
            make.width.equalTo(8)
            make.height.equalTo(titleLab)
        }
        contentLab.snp.makeConstraints { make in
            make.right.equalTo(bg).offset(15)
            make.top.equalTo(bg).offset(6)
            make.left.equalTo(titleLab.snp.right)
            make.height.equalTo(ItemHHeight).priority(666)
            make.bottom.equalTo(bg).offset(-6)
        }
        bg.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.right.equalTo(bg).offset(-8)
            make.height.equalTo(1)
            make.bottom.equalTo(bg).offset(0.5)
        }
        bg.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(8)
            make.width.equalTo(25)
            make.height.equalTo(25)
            make.centerY.equalTo(bg)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
}



final class AuditItemCell2: UITableViewCell {
    private lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        return v
    }()
    
    private let blueBgView = UIView().extBgColor(color: TempDetBg)
    
    var moreBlock: (() -> Void)?
    private lazy var moreBtn: UIButton = {
        let btn = UIButton().easy(nil, UIColor(kshex: "333333"), .systemFont(ofSize: 18, weight: .medium), nil, self, #selector(moreBtnClick))
        
        return btn
    }()
    
    private  lazy var titleLab:UILabel = {
        let v = UILabel()
        v.textColor = UIColor(kshex:"888888")
        v.numberOfLines = 0
        return v
    }()
    lazy var mustLab:UILabel = {
        let v = UILabel()
        v.textColor = UIColor.red
        v.text = "*"
        v.font = UIFont.systemFont(ofSize: 20)
        return v
    }()
    lazy var contentLab:UITextView = {
        let v = UITextView()
        v.font = UIFont.systemFont(ofSize: 16)
        v.textAlignment = .right
        v.backgroundColor = UIColor(kshex: "f5f5f5")
        return v
    }()
    
    private lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex:"#e5e5e5")
        return v
    }()
    private lazy var icon:UIImageView = {
        let v = UIImageView()
        return v
    }()
    var editable = false {
        didSet {
            contentLab.isEditable = editable
//            if editable {
//                contentLab.backgroundColor = UIColor(kshex: "f5f5f5")
//            } else {
//                contentLab.backgroundColor = .white
//
//            }
            
            contentLab.extBgColor(color: TempDetBg)
        }
    }
    var conType:AuditCoTypeEnum = .content {
        didSet {
            self.titleLab.text = titlex
            self.titleLab.extHidden(hidden: conType == .more)
            
     
                
                if contentx.contains("<span") || contentx.contains("<p") {
                    do {
                        if let data = contentx.data(using: String.Encoding.unicode, allowLossyConversion: true) {
                            let attStr = try NSAttributedString.init(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,], documentAttributes: nil)
                            self.contentLab.attributedText = attStr
                        }
                    } catch {
                        self.contentLab.attributedText = nil
                         self.contentLab.text = contentx
                    }
                } else {
                    self.contentLab.attributedText = nil
                     self.contentLab.text = contentx
                }
                 
                
                titleLab.snp.remakeConstraints { make in
                    make.left.equalTo(blueBgView).offset(8)
                    make.centerY.equalTo(blueBgView)
                    make.width.equalTo(100)
                }
            
            // 手动计算文本高度
            var hhh = contentx.heightWithStringAttributes(attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 17)], fixedWidth: KSGlobal.screenW - 140) + 25
            if hhh < ItemHHeight {
                hhh = ItemHHeight
            }
                
                contentLab.snp.remakeConstraints { make in
                    make.right.equalTo(blueBgView).offset(-15)
                    make.top.equalTo(blueBgView).offset(6)
                    make.left.equalTo(titleLab.snp.right).offset(10)
                    make.height.equalTo(hhh)
                    make.bottom.equalTo(bg).offset(-6)
                }
            
            
            
            

            // 标题处理颜色
            
            moreBtn.extHidden(hidden: true)
            
            switch conType {
            case .content:
                icon.isHidden = true
                contentLab.textColor = .black
                contentLab.isUserInteractionEnabled = true

            case .file:
                icon.isHidden = false
                 contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                contentLab.isUserInteractionEnabled = false
                if contentx.contains("pdf") {
                    icon.image = UIImage(named: "icon_pdf")
                } else if contentx.contains("doc") {
                    icon.image = UIImage(named: "icon_word")
                } else if contentx.contains("xls") {
                    icon.image = UIImage(named: "icon_excel")
                } else {
                    icon.image = UIImage(named: "icon_pic")
                }
             case .report:
                icon.isHidden = false
                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
                icon.image = UIImage(named: "icon_report")
            case .underLine:
//                icon.isHidden = true
//                contentLab.textColor = #colorLiteral(red: 0.07058823529, green: 0.5882352941, blue: 0.8588235294, alpha: 1)
//                contentLab.underline()
                break
                case .more:
                    moreBtn.extHidden(hidden: false)
                    moreBtn.extTitle(title: titlex)
                    break
            default:
                break
            }
     
            
            
        }
    }
    var titlex = ""
    var contentx = ""
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = UIColor.clear
        bg.backgroundColor = .white
        self.contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(6)
            make.top.equalTo(self.contentView)
            make.right.equalTo(self.contentView).offset(-6)
            make.bottom.equalTo(self.contentView)
        }
        
        
        bg.addSubview(blueBgView)
        blueBgView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.bottom.equalToSuperview()
        }
        
        blueBgView.addSubview(titleLab)
        
        
        
        blueBgView.addSubview(mustLab)
        
        blueBgView.addSubview(contentLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(blueBgView).offset(8)
            make.centerY.equalTo(blueBgView)
            make.width.equalTo(80)
        }
        mustLab.snp.makeConstraints { make in
            make.right.equalTo(titleLab.snp.left)
            make.centerY.equalTo(titleLab)
            make.width.equalTo(8)
            make.height.equalTo(titleLab)
        }
        contentLab.snp.makeConstraints { make in
            make.right.equalTo(blueBgView).offset(15)
            make.top.equalTo(blueBgView).offset(6)
            make.left.equalTo(titleLab.snp.right)
            make.height.equalTo(ItemHHeight).priority(666)
            make.bottom.equalTo(blueBgView).offset(-6)
        }
        blueBgView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(blueBgView).offset(8)
            make.right.equalTo(blueBgView).offset(-8)
            make.height.equalTo(1)
            make.bottom.equalTo(blueBgView).offset(0.5)
        }
        blueBgView.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.left.equalTo(blueBgView).offset(8)
            make.width.equalTo(25)
            make.height.equalTo(25)
            make.centerY.equalTo(blueBgView)
        }
        
        moreBtn.extHidden(hidden: true)
        blueBgView.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
    }
    
    @objc func moreBtnClick() {
        moreBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {}
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {}
    
}
